# manual_test.py
import time

import redis
import json
from datetime import datetime

# 配置连接到测试环境
r = redis.Redis(db=0)


def send_test_message():
    # 发送入库消息
    insert_payload = {
        "task_type": "insert",
        "payload": json.dumps({
            "plate_number": "测A12345",
            "entry_time": datetime.now().timestamp(),
            "confidence": 0.97
        })
    }
    r.xadd("db_operations_stream", insert_payload)
    print("已发送测试入库消息")

def send_update_message():
    # 发送出库消息
    update_payload = {
        "task_type": "update",
        "payload": json.dumps({
            "plate_number": "测A12345",  # 需要与入库车牌一致
            "exit_time": datetime.now().timestamp(),
            "confidence": 0.95,
            "amount_due": 100  # 测试金额，实际应用中需要根据业务逻辑计算
        })
    }
    r.xadd("db_operations_stream", update_payload)
    print("已发送测试出库消息")

def check_result():
    # 检查数据库记录
    from sqlalchemy import create_engine
    from sqlalchemy.orm import sessionmaker
    from src.models import DetectionResult

    engine = create_engine("mysql+pymysql://root:123456@localhost/plate_detection")
    Session = sessionmaker(bind=engine)

    with Session() as session:
        record = session.query(DetectionResult).filter_by(
            plate_number="测A12345"
        ).first()
        print("当前记录:", record.to_dict() if record else "无记录")


if __name__ == '__main__':
    # 测试完整流程：入库 -> 出库 -> 检查
    send_test_message()
    time.sleep(3)  # 等待入库处理
    send_update_message()
    time.sleep(5)  # 等待出库处理
    check_result()
